home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MOD2TXT.ZIP / CHAP6.TXT < prev    next >
Text File  |  1987-03-25  |  26KB  |  586 lines

  1.                   Chapter 6 - Arrays, Types, and Constants
  2.  
  3.  
  4.              Load  the program named ARRAYS.MOD and we will go right
  5.         to our first example of an array.  An array is simply a list
  6.         made up of several of the same type of element.   Notice the
  7.         VAR  definition  in the sample program and specifically  the
  8.         variable  named  "Automobiles".   The  reserved  word  ARRAY
  9.         followed  by  the square brackets with a  range  of  numbers
  10.         contained  within them is the proper way to define an  array
  11.         of,  in this case, CARDINAL type variables.  This defines 12
  12.         different CARDINAL type variables,  each of which is capable
  13.         of  storing  one CARDINAL number.   The names of the  twelve
  14.         variables are given by Automobiles[1],  Automobiles[2],  ...
  15.         Automobiles[12].  The variable name is "Automobiles" and the
  16.         array  subscripts  are  the  numbers  1  through  12.    The
  17.         variables  are  true  CARDINAL type  variables  and  can  be
  18.         assigned  values,  or they can be used in calculations or in
  19.         nearly  anyplace  in a program where it is legal  to  use  a
  20.         CARDINAL type variable.  One place they cannot be used is as
  21.         the  index  for a FOR loop since a simple variable  type  is
  22.         required there.
  23.  
  24.                            WHAT GOOD ARE ARRAYS?
  25.  
  26.              Notice lines 10 through 12 of the  program.   In  these
  27.         lines,  each of the 12 variables is assigned a value.   When
  28.         "Index"  is 1,  then "Automobiles[1]" is assigned  11,  then
  29.         when "Index" is 2, "Automobiles[2]" is assigned 12, etc.
  30.  
  31.              If  the  12  variables  were  defined  as  12  separate
  32.         variables of whatever names we chose for them,  we could not
  33.         assign  them values in a loop but would have to assign  each
  34.         one  independently.   In  this instance,  we are  generating
  35.         nonsense  data  but in a real program,  this loop  could  be
  36.         reading  in  a series of data from a file such as  would  be
  37.         done with a database.   The advantage of the array should be
  38.         very clear, especially if we were to change the array limits
  39.         to several thousand elements.
  40.  
  41.              The  statement in line 13 assigns a value to one of the
  42.         elements  at random to illustrate the method.   Notice  that
  43.         the 7th element of the array named "Automobiles" is assigned
  44.         the value of 54.   The address of this data is therefore the
  45.         variable  name  "Automobiles[7]" and the data  contained  in
  46.         that  address is 54.   We have therefore assigned values  to
  47.         the 12 variables by a nonsensical but known scheme,  and now
  48.         we can use the 12 variables in any way that is legal  within
  49.         Modula-2.
  50.  
  51.              The  next loop causes the 12 values to be displayed  on
  52.         the  monitor  in  a neat orderly fashion.   In  line  20  we
  53.         display  the index of the variable in question,  and in line
  54.         22,  we display the actual variable.   Keep in mind that the
  55.  
  56.  
  57.                                 Page 37
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                   Chapter 6 - Arrays, Types, and Constants
  68.  
  69.  
  70.         index  could have been INTEGER and still be used to  display
  71.         an array of type CARDINAL provided we defined "Index" as  an
  72.         integer and always used it as such.   Spend enough time with
  73.         this  program  so that you thoroughly  understand  it,  then
  74.         compile and run it.
  75.  
  76.                       WHAT ABOUT AN ILLEGAL SUBSCRIPT?
  77.  
  78.              Modula-2  does  very strong "type checking"  and  limit
  79.         checking.   If,  in the above program, you tried to assign a
  80.         value to "Automobiles[13]",  which doesn't exist, a run time
  81.         error would be generated and the program would cease.   This
  82.         is one of the advantages of Modula-2 over some of the  older
  83.         programming  languages.   Some compilers have the ability to
  84.         enable or disable this feature.
  85.  
  86.                         MULTIPLY DIMENSIONED ARRAYS
  87.  
  88.              Load  the  file named ARRAYS2.MOD for an example  of  a
  89.         program with two-dimensional arrays.   In this program,  the
  90.         VAR  section contains the "Checkerboard" variable  which  is
  91.         defined  as an 8 element ARRAY in which each element is an 8
  92.         element ARRAY, therefore being an 8 by 8 square ARRAY.  Each
  93.         element  is capable of storing one CARDINAL  type  variable.
  94.         The variable "Value" is defined the same way except that the
  95.         method of definition is slightly different.  The two methods
  96.         result in the same type and number of variables.
  97.  
  98.              In  lines  11 through 16 we have two nested FOR  loops.
  99.         The  outer loop causes "Index" to count from 1 to 8 and  for
  100.         each value of "Index",  the variable "Count" counts  through
  101.         the values 1 to 8 also.   The net result is that we evaluate
  102.         the  assignments  in lines 13 and 14 once for each  possible
  103.         combination of "Index" and "Count".   For each  combination,
  104.         we assign some nonsense data to "Checkerboard" then use  the
  105.         result  of that calculation to assign some nonsense data  to
  106.         the variable "Value".  The purpose here is to illustrate the
  107.         method  of using the double subscripted variables.   Next we
  108.         display  the  entire matrix of  "Checkerboard".   The  loops
  109.         cause  8  values  to be displayed on one line  so  that  the
  110.         entire  matrix  is displayed on only 8  lines.   You  should
  111.         study this logic because you will find output sequences like
  112.         this to be very valuable.
  113.  
  114.                         CHANGING A FEW OF THE VALUES
  115.  
  116.              In line 27 and following we change a few of the  values
  117.         at random for illustrative purposes.   Since "Value[3,6]" is
  118.         assigned  the  value  of 3,  it can be used as  one  of  the
  119.         subscripts  of the next line and in fact it is.   This would
  120.         be  a   rather  sloppy programming style but it  is  a  good
  121.  
  122.  
  123.                                 Page 38
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                   Chapter 6 - Arrays, Types, and Constants
  134.  
  135.  
  136.         illustration  of what can be done.   Finally using the  same
  137.         technique as that for "Checkerboard",  the "Value" matrix is
  138.         displayed.
  139.  
  140.                       HOW MANY SUBSCRIPTS CAN BE USED?
  141.  
  142.              There is no limit as to how many subscripts can be used
  143.         in Modula-2 by definition, but there is a practical limit of
  144.         somewhere in the range of 3 or 4.   If you use too many, you
  145.         will  very quickly get confused and lose control of what the
  146.         program  is supposed to be doing.   I have never  seen  more
  147.         than 3 subscripts used in any programming language, and very
  148.         few instances of more than two.   Let the problem definition
  149.         be your guide.
  150.  
  151.              This program was pretty straightforward, and it is time
  152.         for you to compile and run it.
  153.  
  154.                             THE TYPE DECLARATION
  155.  
  156.              Load  the program named TYPES.MOD for a new topic  that
  157.         you will use often,  especially in large programs.   At  the
  158.         top  of  the listing we have a group of  TYPE  declarations.
  159.         The  first line defines "ArrayDef" as a new TYPE  definition
  160.         that  can  be used in the same way you would use INTEGER  or
  161.         any of the other simple type definitions.   In line 12,  the
  162.         variable  named  "Stuff" is defined as a  variable  of  type
  163.         "ArrayDef", and since "ArrayDef" is a 14 element ARRAY, then
  164.         "Stuff" is a 14 element array of INTEGER.   It seems like we
  165.         didn't  save anything and in fact we added a few  keystrokes
  166.         to the program in order to do this.   If you look at line 13
  167.         you  will see that we have also defined "Stuff2" as the same
  168.         type of array.   We have,  in fact, defined them to be "type
  169.         compatible" which will be very important when we get to  the
  170.         program itself.
  171.  
  172.              Continuing  down  the  list of  TYPE  declarations,  we
  173.         define a TYPE with 28 characters,  then a TYPE with 60  real
  174.         variables,  and another with 6 BOOLEAN variables.   The next
  175.         TYPE  consists  of 12 variables of TYPE "DogFood"  which  is
  176.         itself a TYPE of 6 BOOLEANS,  resulting in a TYPE consisting
  177.         of  6  times 12 = 72 BOOLEANS.   It is possible to  continue
  178.         building up TYPE definitions like this indefinitely,  and as
  179.         you build up applications,  you will find yourself  building
  180.         up  rather  complex  TYPE declarations and  having  a  clear
  181.         picture  of how they go together because it is your solution
  182.         to  a problem.   The last TYPE to be defined is  that  named
  183.         "Boat"  which has exactly the same size and  characteristics
  184.         as  "Airplane".   We  will  see  shortly  that  there  is  a
  185.         difference in these two definitions.
  186.  
  187.  
  188.  
  189.                                 Page 39
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                   Chapter 6 - Arrays, Types, and Constants
  200.  
  201.  
  202.  
  203.                          HOW DO WE USE ALL OF THIS?
  204.  
  205.              In  the VAR part of the definition part of the program,
  206.         we declare some variables,  two simple types and some of the
  207.         types we defined above.  In the program part, we assign some
  208.         values  to the 72 variables making up the  "Puppies"  matrix
  209.         and the 72 variables making up the "Kitties" matrix.  All of
  210.         the  elements of "Stuff" are then assigned nonsense  values.
  211.         The  really interesting statement comes in line 30 where  we
  212.         say  "Stuff2 := Stuff;".   In this simple statement,  all 14
  213.         values   stored   in   "Stuff"  are  copied  into   the   14
  214.         corresponding  elements  of "Stuff2" without using  a  loop.
  215.         This  is  possible  because  the  two  variables  are   TYPE
  216.         compatible,  they  have  the same TYPE definition.   If  you
  217.         study the definitions above,  you will see that "Stuff3"  is
  218.         of  the same number and range of elements and is composed of
  219.         the same type of elements,  namely INTEGER,  as "Stuff"  but
  220.         they  are not TYPE compatible because were not defined  with
  221.         the  same TYPE definition statement.   In like manner,  even
  222.         though "Puppies" and "Kitties" are identical in  type,  they
  223.         are not TYPE compatible.
  224.  
  225.              You  have the ability,  through careful  assignment  of
  226.         variables, to avoid certain kinds of programming errors.  If
  227.         certain variables should never be assigned to each other,  a
  228.         careful  selection  of types can prevent  it.   Suppose  for
  229.         example  that  you have a program working with  peaches  and
  230.         books.   You would never want to copy a matrix of peaches to
  231.         one defining books,  it just wouldn't make sense.  Those two
  232.         matrices  should be defined with different type declarations
  233.         even though they may be identical in size.
  234.  
  235.              Compile  and  run  this program,  even though  it  will
  236.         result in no output, then move the comment delimiter in line
  237.         31 to a position following the assignment statement and  see
  238.         if it does give you a TYPE incompatibility error.
  239.  
  240.                             DEFINING A CONSTANT
  241.  
  242.              Load the program named CONSTANT.MOD for a definition of
  243.         the constant as used in Modula-2.   We will finally keep the
  244.         promise made when we studied LOOPDEMO in chapter 4.  The new
  245.         reserved  word CONST is used to define a constant for use in
  246.         the program.  The constant "MaxSize" can be used anywhere in
  247.         the program that it is desired to use the number 12, because
  248.         they are in fact identical.  Two additional CONST values are
  249.         defined  for  illustrative  purposes  only.    In  the  TYPE
  250.         declaration  section we use the constant "MaxSize" to define
  251.         two types, then use them to define several variables.
  252.  
  253.  
  254.  
  255.                                 Page 40
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                   Chapter 6 - Arrays, Types, and Constants
  266.  
  267.  
  268.              In  the  program there is one FOR loop using  the  same
  269.         constant "MaxSize" as the upper limit.   It doesn't seem  to
  270.         be  too  useful yet,  but suppose your boss came to you  and
  271.         said  to  change the program so that it  handled  142  cases
  272.         instead  of 12.   The way the program is written,  you would
  273.         only  have to change the value of the  constant,  recompile,
  274.         and  you  would  be done.   If you had used  the  number  12
  275.         everywhere,  you would have to replace every 12 with the new
  276.         number,  142, being careful not to change the one in line 21
  277.         which is a different kind of 12.   Of course even that would
  278.         not  be  too difficult in such a simple program,  but  in  a
  279.         program  with 5000 lines of code,  one simple  change  could
  280.         take a week.
  281.  
  282.              Compile and run this program.
  283.  
  284.  
  285.                        THE OPEN ARRAY IN A PROCEDURE
  286.  
  287.              Load and display the program named ARAYPASS.MOD for  an
  288.         example  of  a  program  with  arrays  being  passed  to   a
  289.         procedure.   Notice  how the procedures are formatted.   The
  290.         rows  of  asterisks make them really stand out and  easy  to
  291.         find.    You  will  develop  your  own  personal  style   of
  292.         formatting  in  a way that is clear and easy to  follow  for
  293.         you.
  294.  
  295.              The two procedures in this program are identical except
  296.         for  the  way the arrays are passed to them.   In the  first
  297.         procedure named "AddNumbers", the variable named "Donkey" is
  298.         passed  the array by using the same type which was  used  to
  299.         define  one  of the arrays.   The procedure merely adds  the
  300.         values  of the elements of the array passed to it and writes
  301.         the result out to the monitor.  The way it is written, it is
  302.         only  capable of adding arrays that are indexed from  10  to
  303.         15.  Any other array will cause a "type incompatible" error.
  304.         This is simply called passing an array to the procedure.
  305.  
  306.              The  second  procedure  named "GenAddNumbers"  has  its
  307.         input array defined as an "ARRAY OF CARDINAL" with no limits
  308.         stated.   This procedure can add all of the variables in any
  309.         CARDINAL  array  regardless of the range of its  subscripts.
  310.         The  lower subscript will always be defined as  zero  within
  311.         this type of procedure, and the upper limit of the array can
  312.         be  found with the predefined procedure "HIGH".   It is used
  313.         as shown in the example.   The first time this procedure  is
  314.         called  in the main program,  it is called with the variable
  315.         "SizeOne".   In  the  procedure,  the array  subscripts  for
  316.         "Donkey"  will  be 0 through 5.   When  the  variable  named
  317.         "SizeTwo" is the array sent to the procedure,  then "Donkey"
  318.         will  have  the limits of 0 and 218.   The second  procedure
  319.  
  320.  
  321.                                 Page 41
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                   Chapter 6 - Arrays, Types, and Constants
  332.  
  333.  
  334.         definition method is therefore more general.  This is called
  335.         passing an "open array" to the procedure.
  336.  
  337.                           WHICH ONE SHOULD I USE?
  338.  
  339.              There  will be times when you wish to use  the  general
  340.         case  for  passing a parameter,  the "open array".   A  good
  341.         example  is the procedure named "WriteString" that  we  have
  342.         been  using in this tutorial.   It would be a bit cumbersome
  343.         if  we were only allowed to pass a 10 character string to it
  344.         each time.   Since it can accept a string of any length,  it
  345.         is evidently defined with an "ARRAY OF CHAR" in its  header.
  346.         (We  will  see  in  a later  chapter  that  this  particular
  347.         procedure  is  exactly that,  a procedure that  someone  has
  348.         thoughtfully programmed for you.   You only need to tell the
  349.         system where it can be found using the IMPORT statement.)
  350.  
  351.              There  will  likewise be times when you will desire  to
  352.         use  the  more specific method of definition.   If  you  are
  353.         using  a  lot of arrays and have a specific  operation  that
  354.         needs  to  be done to only a few arrays that have  a  common
  355.         definition,  you  would  be wise to use  this  method.   The
  356.         computer  could  then  tell  you if you  tried  to  use  the
  357.         procedure on an array that it was not intended for.  This is
  358.         making  wise  use  of the type  checking  available  in  the
  359.         computer.
  360.  
  361.                           HANDLING STRINGS IN MODULA-2
  362.  
  363.              Load  the last file for this chapter,  STRINGEX.MOD for
  364.         an  example of using strings in Modula-2.   This program  is
  365.         the  first program to deviate from the standard  library  as
  366.         defined by Niklaus Wirth.   When he defined the language, he
  367.         suggested   several   library  procedures  that  should   be
  368.         available  in  every  Modula-2 compiler  and  most  compiler
  369.         writers  have followed his suggestions  quite  closely.   He
  370.         failed  to define a standard library for the string handling
  371.         procedures.   There  is  therefore  some  freedom  for  each
  372.         compiler  writer to define the string handling  routines  in
  373.         any way he pleases.   Most however, have followed at least a
  374.         resemblance  to a standard,  so the procedure calls are very
  375.         similar from compiler to compiler.   It may be necessary for
  376.         you  to modify this file to suit your  particular  compiler.
  377.         The COMPILER.DOC file on your distribution disk has comments
  378.         for modifications needed for several compilers, but if yours
  379.         is  not listed,  it will be up to you to make  the  required
  380.         modifications to the source file.
  381.  
  382.              A  complete description of the libraries and what  they
  383.         are will be given in chapter 8.
  384.  
  385.  
  386.  
  387.                                 Page 42
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                   Chapter 6 - Arrays, Types, and Constants
  398.  
  399.  
  400.                      BACK TO THE PROGRAM ON YOUR DISPLAY
  401.  
  402.              The  first thing that is different here is the addition
  403.         of another IMPORT statement in line 10,  this one  importing
  404.         procedures  from the module named "Strings".   This  is  the
  405.         module containing the procedures which we will need in  this
  406.         program.  A string is an array of type CHAR, each element of
  407.         the  array being capable of storing one character.   Thus an
  408.         array of CHAR type elements is capable of storing a word,  a
  409.         sentence, a paragraph, or even a whole chapter, depending on
  410.         how big the array is.   Using the example on your screen, we
  411.         will learn how to manipulate text data.
  412.  
  413.              One  additional feature of the example program will  be
  414.         found on line 24.   In this line the "WriteString" procedure
  415.         is used in a way we have not used as yet.  Instead of having
  416.         an  expression  in  quotes,  it has the name of  a  variable
  417.         within its parentheses.  It will display whatever characters
  418.         are stored in the string named "stuff" defined by the "ARRAY
  419.         OF CHAR".   So if we learn how to get a string of characters
  420.         stored  in  a  variable of type  "string",  we  can  display
  421.         anything on the monitor that we can generate internal to the
  422.         computer.
  423.  
  424.              According to the definition of Modula-2, a string is an
  425.         ARRAY  OF CHAR with a 0 as a terminator.   We will get  more
  426.         familiar with strings as we continue our study.
  427.  
  428.                          SOME NEW STRING PROCEDURES
  429.  
  430.              The first line of the program itself, line 34, contains
  431.         a  string  assignment.   In this case,  we are  telling  the
  432.         system to copy the constant "ABCDEFGHIJKL" into the variable
  433.         named  "Horse".   The array into which you are copying  must
  434.         begin  at  index  0 in order for this to  work  because  all
  435.         character  constants are,  by definition,  started at  zero.
  436.         The  variable  "Horse",  which  only contains  room  for  12
  437.         characters  will only receive the first 12 characters of the
  438.         constant.   The procedure "Display" is called with Horse  as
  439.         the   variable   and  the  variable  is  displayed   between
  440.         parentheses  for  clarity  of  understanding,   and  the  12
  441.         characters  of  the variable are displayed  in  their  ASCII
  442.         equivalent.  When you finally run this program, compare some
  443.         of  the values to the ASCII table that is included with  the
  444.         DOS documentation that came with your computer.
  445.  
  446.              In  line  37 of the program,  the constant  "12345"  is
  447.         assigned  to  the variable "Cow".   In the  next  line,  the
  448.         variable "Cow" is assigned to the variable "Horse",  and the
  449.         display procedure is called again.   This time, the variable
  450.         "Cow" is shorter than the destination,  so the system has to
  451.  
  452.  
  453.                                 Page 43
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                   Chapter 6 - Arrays, Types, and Constants
  464.  
  465.  
  466.         compensate  for the difference.   After it transfers  the  5
  467.         characters to "Horse",  it will place a 0 (zero) in the next
  468.         position to indicate the end of the string.   The definition
  469.         of  the  string still has 12 places,  but there are  only  5
  470.         places  of interest,  so the system will consider all places
  471.         past the 5th as undefined.  This time the system only prints
  472.         out  5  characters  in the procedure.   The  list  of  ASCII
  473.         equivalents shows that the other values are still there, the
  474.         output  routine simply stopped when it came to the 0 in  the
  475.         sixth position.
  476.  
  477.              Note  that  the Assign statement may be  different  for
  478.         different compilers because it is not a part of the Modula-2
  479.         definition by Niclaus Wirth.
  480.  
  481.                                CONCATENATION
  482.  
  483.              Concatenation is simply putting two strings together to
  484.         make up one bigger string.   Beginning in line 41,  two  new
  485.         string  variables are defined,  "S1" and "S2",  then the two
  486.         new variables are concatenated together and assigned to  our
  487.         old  favorite variable named "Horse".   The variable "Horse"
  488.         should  now contain the silly expression  "NeatThings",  and
  489.         when you run the program,  you will find that it  does.   It
  490.         also  has  a 0 in character position 11 now to indicate  the
  491.         end  of the string.   Line 47 concatenates "Horse" to  "Cow"
  492.         and stores the result in "Horse",  but since the  expression
  493.         is  now too long,  part of it will get truncated and  simply
  494.         thrown away.  Finally, "Cow" is concatenated to "Horse", and
  495.         the result stored back into "Horse".  This has the effect of
  496.         shifting  the prior contents of "Horse" right and adding the
  497.         characters stored in "Cow" to the beginning.   Line 45 is an
  498.         example  of  a string assignment.   This  is  only  possible
  499.         because they are of the same TYPE.  The variable "Cow" has a
  500.         different  TYPE so can't be assigned to either of these  two
  501.         variables.   Note  that  the TYPE does not have to start  at
  502.         zero for this to work.
  503.  
  504.              Note  that  even though "Horse" was the  only  variable
  505.         used  in the calls to "Display",  any of the  other  strings
  506.         could have been used also.   This is the topic of the fourth
  507.         programming exercise below.
  508.  
  509.              Compile  and run the program and see if it really  does
  510.         do all that it should do as described above, keeping in mind
  511.         that  you  may have to modify the file to  accommodate  your
  512.         particular compiler.
  513.  
  514.  
  515.         PROGRAMMING EXERCISES
  516.  
  517.  
  518.  
  519.                                 Page 44
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                   Chapter 6 - Arrays, Types, and Constants
  530.  
  531.  
  532.         1.   Write a program to store the CARDINAL values 201 to 212
  533.              in an array then display them on the monitor.
  534.  
  535.         2.   Write  a  program to store a 10 by 10 array  containing
  536.              the products of the indices, therefore a multiplication
  537.              table.  Display the matrix on the monitor.
  538.  
  539.         3.   Modify the program in 2 above to include a constant  so
  540.              that  by simply changing the constant,  the size of the
  541.              matrix and the range of the table will be changed.
  542.  
  543.         4.   Modify  the program named STRINGEX.MOD to include calls
  544.              to  "Display" with each of the string variables at  the
  545.              end of the program.
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.                                 Page 45
  586.